<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>login</title>
    <style type="text/css">
        *{
            margin: 0;
            padding: 0;
        }
        #wrap {
            height: 719px;
            width: 100;
            background-repeat: no-repeat;
            background-position: center center;
            position: relative;
        }
        #head {
            height: 80px;
            width: 100;
            background-color: white;
            text-align: center;
            position: relative;
        }
        #foot {
            width: 100;
            height: 126px;
            background-color: white;
            position: relative;
        }
        #wrap .logGet {
            height: 408px;
            width: 368px;
            position: absolute;
            background-color: #FFFFFF;
            top: 20%;
            right: 10%;
        }
        #submit{
            width: 100%;
            height: 45px;
            background-color: #ee7700;
            border: none;
            color: white;
            font-size: 18px;
        }
        .logGet .logD.logDtip .p1 {
            display: inline-block;
            font-size: 28px;
            margin-top: 30px;
            width: 86%;
        }
        #wrap .logGet .logD.logDtip {
            width: 86%;
            border-bottom: 1px solid #ee7700;
            margin-bottom: 60px;
            margin-top: 0px;
            margin-right: auto;
            margin-left: auto;
        }
        .logGet .lgD img {
            position: absolute;
            top: 12px;
            left: 8px;
        }
        .logGet .lgD input {
            width: 100%;
            height: 42px;
            text-indent: 2.5rem;
        }
        #wrap .logGet .lgD {
            width: 86%;
            position: relative;
            margin-bottom: 30px;
            margin-top: 30px;
            margin-right: auto;
            margin-left: auto;
        }
        #wrap .logGet .logC {
            width: 86%;
            margin-top: 0px;
            margin-right: auto;
            margin-bottom: 0px;
            margin-left: auto;
        }


        .title {
            font-family: 微软雅黑;
            font-size: 26px;
            color: black;
            position: absolute;
            background-color: white;
            top: 50%;
            left: 7%;
            transform: translate(-50%, -50%);  /* 使用css3的transform来实现 */
            font-size: 36px;
            height: 40px;
            width: 30%;
        }

        .copyright {
            font-family: "宋体";
            color: black;
            position: absolute;
            background-color: white;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);  /* 使用css3的transform来实现 */
            height: 60px;
            width: 40%;
            text-align:center;
        }


        #foot .copyright .img {
            width: 100%;
            height: 24px;
            position: relative;
        }
        .copyright .img .icon {
            display: inline-block;
            width: 24px;
            height: 24px;
            margin-left: 22px;
            vertical-align: middle;
            background-image: url(%E7%94%B5%E5%AD%90%E9%82%AE%E4%BB%B6.png);
            background-repeat: no-repeat;
            vertical-align: middle;
            margin-right: 5px;
        }

        .copyright .img .icon1 {
            display: inline-block;
            width: 24px;
            height: 24px;
            margin-left: 22px;
            vertical-align: middle;
            background-image: url(%E5%9C%B0%E5%9D%80.png);
            background-repeat: no-repeat;
            vertical-align: middle;
            margin-right: 5px;
        }
        .copyright .img .icon2 {
            display: inline-block;
            width: 24px;
            height: 24px;
            margin-left: 22px;
            vertical-align: middle;
            background-repeat: no-repeat;
            vertical-align: middle;
            margin-right: 5px;
        }
        #foot .copyright p {
            height: 24px;
            width: 100%;
        }
    </style>
    <script type="text/javascript" src="/js/jquery-3.2.1.min.js"></script>
</head>


<div class="wrap" id="wrap">
    <div class="logGet">
        <form method="post">
            <!-- 头部提示信息 -->
            <div class="logD logDtip">
                <p class="p1">登录</p>
            </div>
            <!-- 输入框 -->
            <div class="lgD">
                <img src="../images/name.png" width="20" height="20" alt=""/>
                <input type="text"
                       placeholder="邮箱" name="email" id="email"/>
            </div>
            <div class="lgD">
                <img src="../images/pwd.png" width="20" height="20" alt=""/>
                <input type="password"
                       placeholder="密码" name="password" id="password"/>
            </div>
            <div class="lgD">
                <img src="../images/pwd.png" width="20" height="20" alt=""/>
                <input type="password"
                       placeholder="确认密码" name="ConfirmPassword" id="ConfirmPassword"/>
            </div>
            <div class="lgD">
                <img src="../images/pwd.png" width="20" height="20" alt=""/>
                <input type="code"
                       placeholder="验证码" name="code" id="code"/>
                <input type="button" id="getCode" value="获取验证码"></input>
            </div>
            <div class="logC">
                <input type="button" id="submit" value="注册"/>
            </div>
        </form>
    </div>
</div>
<script type="text/javascript">
    var time = 60;
    var flag = true;
    $('#getCode').click(function(){
        var email = $("#email").val();
        if(email==""){
            alert("请输入邮箱");
            $("#email").focus();
            return false;
        }
        $(this).attr("disabled",true);
        if(flag){
            var timer = setInterval(function () {
                if(time == 60 && flag){
                    flag = false;
                    $.ajax({
                        type: 'get',
                        url : 'http://localhost:80/api/code/create/'+email,
                        success : function(data) {
                            if(data.code == 200){
                                $("#getCode").html("已发送");
                            }else {
                                alert(data.msg);
                                flag = true;
                                time = 60;
                                clearInterval(timer);
                            }
                        }
                    });
                }else if(time == 0){
                    $("#getCode").removeAttr("disabled");
                    $("#getCode").val("获取验证码");
                    clearInterval(timer);
                    time = 60;
                    flag = true;
                }else {
                    $("#getCode").val(time + " s 重新发送");
                    time--;
                }
            },1000);
        }

    })
    
    $("#submit").click(function () {
        // 校验密码和确认密码是否一致
        var password = $("#password").val();
        var ConfirmPassword = $("#ConfirmPassword").val();
        if(password!=ConfirmPassword){
            alert("两次密码输入不一致");
        }
        var email = $("#email").val();
        var code = $("#code").val();
        if(email==""){
            alert("邮箱不能为空");
            $("#email").focus();
            return false;
        }
        if(code==""){
            alert("验证码不能为空");
            $("#code").focus();
            return false;
        }
        $.ajax({
            url: "http://localhost:80/api/user/register/"+email+"/"+password+"/"+code,
            type: "get",
            success: function (data) {
                alert(data.msg);
                if(data.code=200){
                    location.href="login.html";
                }
            }
        })
    })
    $(document).ready(function() {

        $("#email").blur(function () {
            var email = $("#email").val();
            // 校验邮箱是否注册过
            $.ajax({
                url: "http://localhost:80/api/user/isRegistered/" + email,
                method: "get",
                success: function (data) {
                    if (data == true) {
                        // 提示
                        $("#code").attr("disabled",true);
                        $("#submit").attr("disabled",true);
                        alert("邮箱已被注册！")
                        return false;
                    } else{
                        $("#code").removeAttr("disabled");
                        $("#submit").removeAttr("disabled");
                    }
                },
                error: function (data) {
                    alert("请求出错！");
                }
            })
        })
    })
</script>

</body>
</html>